class DataObject < ActiveRecord::Base
  belongs_to :location
  belongs_to :data_provider
  belongs_to :taxon
  belongs_to :chapter
  belongs_to :toxin
  belongs_to :harmful_effect
  
  #Returns a hash of data provider and a list of data objects
  def self.find_taxon_specific_linkouts(taxon_id)
    #A cut and paste job from below
    taxon_retrieval_results = {}
    provider_search_params = {:taxon_id => taxon_id, :content=>nil} #The only significant difference from the routine below
    conditions = provider_search_params
    data_object_search_for_providers = self.find(:all, :select => "data_provider_id", :conditions => conditions, :group => "data_provider_id")
    data_object_search_for_providers.each do |search_for_provider_result|
      #Get information about the data provider
      data_provider = search_for_provider_result.data_provider
      data_objects = data_provider.data_objects.find(:all, :conditions=> conditions)
      taxon_retrieval_results[data_provider] = data_objects
    end
    return taxon_retrieval_results
  end

  #Give back data objects that have text in them (as opposed to linkouts)
  def self.find_taxon_specific_text_content(taxon_id)
    #Grab a list of data providers that have information about a given taxon
    taxon_retrieval_results = {}
    provider_search_params = {:taxon_id => taxon_id}
    conditions = ["taxon_id = :taxon_id and content is not null", provider_search_params]
    data_object_search_for_providers = self.find(:all, :select => "data_provider_id", :conditions => conditions, :group => "data_provider_id")
    data_object_search_for_providers.each do |search_for_provider_result|
      #Get information about the data provider
      data_provider = search_for_provider_result.data_provider
      data_objects = data_provider.data_objects.find(:all, :conditions=> conditions)
      taxon_retrieval_results[data_provider] = data_objects
    end
    return taxon_retrieval_results
  end


  #find_general_linkouts will list all linkouts in a parent node (ie algae) 

  #"harmful effect" is a little confusing - it refers to the chapter heading in the IOC reference list,
  #rather than to actual the class HarmfulEffect
  def self.find_harmful_effects_not_linked_to_toxins
    harmful_effect_chapter = Chapter.find_by_name("Harmful effect")
    harmful_effect_data_objects = self.find(:all, :conditions=>["chapter_id = :chapter_id and content is not null", {:chapter_id=>harmful_effect_chapter}])
    toxin_reconciliation_list = ToxinReconciliation.find(:all)
    harmful_effects_not_linked_to_toxins = []
    harmful_effect_data_objects.each do |harmful_effect_data_object|
      reconciliation_found = FALSE
      toxin_reconciliation_list.each do |toxin_reconciliation|
        if (harmful_effect_data_object.content.include?(toxin_reconciliation.verbatim_phrase))
	  reconciliation_found = TRUE
	  break
	end
      end
      if (not reconciliation_found)
	harmful_effects_not_linked_to_toxins << harmful_effect_data_object
      end
    end
    return harmful_effects_not_linked_to_toxins
  end
  
  #"harmful effect" is a little confusing - it refers to the chapter heading in the IOC reference list,
  #rather than to actual the class HarmfulEffect
  def self.find_harmful_effects_not_linked_to_diseases
    return nil #Refers to objects that haven't been written yet
    harmful_effect_chapter = Chapter.find_by_name("Harmful effect")
    harmful_effect_data_objects = self.find(:all, :conditions=>["chapter_id = :chapter_id and content is not null", {:chapter_id=>harmful_effect_chapter}])
    disease_reconciliation_list = HarmfulEffectReconciliation.find(:all)
    harmful_effects_not_linked_to_diseases = []
    harmful_effect_data_objects.each do |harmful_effect_data_object|
      reconciliation_found = FALSE
      disease_reconciliation_list.each do |disease_reconciliation|
        if (harmful_effect_data_object.content.include?(disease_reconciliation.verbatim_phrase))
	  reconciliation_found = TRUE
	  break
	end
      end
      if (not reconciliation_found)
	harmful_effects_not_linked_to_diseases << harmful_effect_data_object
      end
    end
    return harmful_effects_not_linked_to_diseases
  end
  
  
  validates_presence_of :chapter_id, :location_id, :taxon_id, :data_provider_id 
  validates_uniqueness_of :chapter_id, :scope=> [:url]
end
